﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace CSharp.Infrastructure
{
    /// <summary>
    /// 图片处理类
    /// </summary>
    public class ImageHelper
    {
        /// <summary>
        /// 添加文字水印
        /// </summary>
        /// <param name="image"></param>
        /// <param name="user_name"></param>
        /// <param name="addr"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        public static Image AddTextToImg(Image image, string addr, string time)
        {
            // 画水印图
            var bm = DrawWater(addr, time);

            Bitmap bitmap = new Bitmap(image, image.Width, image.Height);
            Graphics g = Graphics.FromImage(bitmap);

            // 按比例重画水印图片
            int waterWidth = bitmap.Width * 3 / 4;
            int waterHeight = bm.Height * waterWidth / bm.Width;
            var waterBmp = ZoomImage(bm, waterWidth, waterHeight);

            // 把水印加到图上去
            Rectangle rect = new Rectangle(0, bitmap.Height - waterHeight, waterWidth, waterHeight);
            g.DrawImage(waterBmp, rect);
            g.Dispose();

            Image img = bitmap;
            return img;
        }

        /// <summary>
        /// 画水印
        /// </summary>
        /// <param name="addr">地址</param>
        /// <param name="time">时间</param>
        /// <param name="username">项目负责人（拍摄者）</param>
        /// <returns></returns>
        public static Bitmap DrawWater(string addr, string time)
        {
            int rectX = 10;
            int rectY = 10;
            float fontSize = 18; // >字体大小
            int opacity = 200;  // 0-255 值越大透明度越低
            FontFamily ff = null;
            if (System.Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                ff = new FontFamily(@"楷体");
            }
            else
            {
                ff = new FontFamily(@"/usr/share/fonts/truetype/simkai.ttf");
            }

            Font font = new Font(ff.Name, fontSize, FontStyle.Bold); //定义字体
            Font font1 = new Font(ff.Name, 14, FontStyle.Bold); //定义字体
            Font font2 = new Font(ff.Name, 14, FontStyle.Bold); //定义字体

            Brush redBrush = new SolidBrush(Color.FromArgb(opacity, 255, 255, 255)); //画文字用

            Bitmap bmp = new Bitmap(420, 160); // 创建画布 420 * 310
            Graphics g = Graphics.FromImage(bmp);
            g.Clear(Color.Transparent);

            StringFormat format = new StringFormat(StringFormatFlags.NoClip);
            format.Alignment = StringAlignment.Near;
            format.LineAlignment = StringAlignment.Center;

            int lineWidth = 410;
            int lineHeight = 30;

            Rectangle rect = new Rectangle(rectX, rectY, lineWidth, lineHeight);
            g.DrawString($"时 间：{time}", font1, redBrush, rect, format);
            rectY += lineHeight;

            rect = new Rectangle(rectX, rectY, lineWidth, lineHeight);
            g.DrawString($"地 点：{addr}", font2, redBrush, rect, format);
            g.Dispose();

            return bmp;
        }

        /// <summary>
        /// 比例缩放图片
        /// </summary>
        /// <param name="sourceBm"></param>
        /// <param name="waterWidth"></param>
        /// <param name="waterHeight"></param>
        /// <returns></returns>
        public static Bitmap ZoomImage(Bitmap sourceBm, int waterWidth, int waterHeight)
        {
            Bitmap waterBmp = new Bitmap(waterWidth, waterHeight); // 按比例重画后的图片
            Graphics g1 = Graphics.FromImage(waterBmp);
            g1.Clear(Color.Transparent);
            // 设置画布的描绘质量   
            g1.CompositingQuality = CompositingQuality.HighQuality;
            g1.SmoothingMode = SmoothingMode.HighQuality;
            g1.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g1.DrawImage(sourceBm, new Rectangle(0, 0, waterWidth, waterHeight + 1), 0, 0, sourceBm.Width, sourceBm.Height, GraphicsUnit.Pixel);
            g1.Dispose();

            return waterBmp;
        }
    }
}
